<script setup>
import { ref, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { useGameStore, useSortStore } from '@/stores/index'
import router from '@/router'
import { baseURL } from '@/utils/request'
/* const imgList = ref([])
const getGame = async () => {
  const res = await getGameList()
  imgList.value = res.data
} */
const gameStore = useGameStore()
const sortStore = useSortStore()
sortStore.getSortList()
gameStore.getApprovedGameList()
const game_name = ref('')
const category = ref('')
const loading = ref(false)
/* getGame() */
onMounted(() => {
  console.log(gameStore.game)
})
const goDetail = (id) => {
  router.push({ name: 'GameDetail', params: { id } })
}

//点击搜索按钮，轮播图消失，搜索游戏出现

const search = async (game_name, category) => {
  console.log('搜索', game_name, category)
  loading.value = true
  setTimeout(() => {
    if (game_name || category) {
      const carousel = document.querySelector('.carousel-1')
      carousel.style.display = 'none'
      gameStore.searchGame({ game_name, category })
      loading.value = false
    } else {
      gameStore.getGamelist()
      const carousel = document.querySelector('.carousel-1')
      carousel.style.display = 'block'
      loading.value = false
    }
  }, 1000)
}
</script>

<template>
  <!-- 中间游戏列表 -->
  <div class="main-container">
    <!-- 搜索框 -->
    <div class="search-1">
      <el-input
        v-model="game_name"
        style="max-width: 600px"
        placeholder="请输入游戏名称"
        class="input-with-select"
        size="large"
        @keyup.enter="search(game_name, category)"
      >
        <template #prepend>
          <el-select v-model="category" placeholder="分类" style="width: 115px" size="large">
            <el-option
              v-for="item in sortStore.sort"
              :key="item.id"
              :label="item.category"
              :value="item.category"
            />
          </el-select>
          <el-icon v-if="category" class="el-input__icon" @click="category = ''"
            ><CircleCloseFilled
          /></el-icon>
        </template>
        <template #append>
          <el-button :icon="Search" @click="search(game_name, category)" />
        </template>
      </el-input>
    </div>

    <div class="game_list" v-loading="loading" element-loading-text="正在加载中...">
      <!-- 轮播图 -->
      <div class="carousel-1">
        <el-carousel :interval="4000" type="card" height="300px" arrow="always">
          <el-carousel-item v-for="item in gameStore.game.slice(0, 4)" :key="item.id">
            <img
              :src="baseURL + '/files/' + item.game_image"
              @click="goDetail(item.id)"
              alt="item.game_name"
            />
            <h3 text="2xl" justify="center">{{ item.game_name }}</h3>
          </el-carousel-item>
        </el-carousel>
      </div>
      <!-- 游戏列表 -->

      <ul v-if="gameStore.game.length > 0">
        <li v-for="item in gameStore.game" :key="item.id">
          <el-card class="card" shadow="hover" @click="goDetail(item.id)">
            <template #footer>
              <div class="title">{{ item.game_name }}</div>
              <div class="price">￥{{ item.price }}</div>
            </template>
            <img :src="baseURL + '/files/' + item.game_image" style="width: 100%" />
          </el-card>
        </li>
      </ul>
      <template v-else>
        <el-empty
          description="🐰兔子君🐰翻遍了整个游戏窝，都没有啊找到你要的游戏，换个关键字试试吧꒰⑅•ᴗ•⑅꒱⸝♪✰"
        />
      </template>
    </div>
  </div>
</template>

<style scoped lang="scss">
.el-empty .el-empty__description p {
  font-size: 24px !important;
}
.search-1 {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
  margin-top: 10px;

  .input-with-select {
    border: #ffffff 0.5px solid;
    border-radius: 4px;
    background-color: #1989fa !important ;
  }

  /*   .input-with-select .el-button:hover {
    background-color: #fff;
  } */
}

.input-with-select .el-input-group__prepend {
  background-color: #1989fa;
}

.demo-tabs .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.el-carousel__item {
  h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
  }

  img {
    width: 100%;
    height: 300px;
    object-fit: cover;
    object-position: center;
    border-radius: 10px;
  }
}

.carousel-1 {
  padding: 30px 0;
  border-top: rgb(64, 158, 255) 1px solid;
  border-bottom: rgb(64, 158, 255) 1px solid;

  background-color: rgb(245, 248, 250);
}

.el-carousel__item .el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
  border-radius: 10px;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
  border-radius: 10px;
}

.main-container {
  background-color: #fff;
  border-radius: 0 0 10px 10px;
  margin: 0 auto;
  width: 1200px;

  padding-top: 20px;

  ul {
    width: 100%;
    display: flex;

    flex-wrap: wrap;

    li:nth-child(4n + 1) {
      margin-left: 0;
    }
    li:nth-child(4n) {
      margin-right: 0;
    }
    li {
      margin: auto 36px;
    }
    .card {
      width: 245px;
      border: rgb(255, 255, 255) 1px solid;

      img {
        border-radius: 10px;
      }
    }

    .card:hover {
      border: rgb(64, 158, 255) 1px solid;
    }

    .title {
      margin-bottom: 5px;
      font-size: 18px;
      font-weight: 600;
      color: #333;
    }

    .price {
      font-size: 18px;
      font-weight: 600;
      color: #ff4d4f;
    }
  }
}
</style>
